/**
 * 
 */
package cardbs.dao;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import cardbs.domain.Department;
import cardbs.domain.Deptype;
import cardbs.domain.Part;
import cardbs.domain.ServicePoint;
import cardbs.domain.Work;


/** 
 * <!-- begin-UML-doc -->
 * <!-- end-UML-doc -->
 * @author marianna
 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class PartDAO extends GenericDAO<Part> {
	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param partName
	 * @return
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public Boolean findPart(String partName) {
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select part from Part part where partName=:insertedPartName");
	    query.setParameter("insertedPartName", partName);
	    List <Part> results = query.getResultList();
	    int size = results.size();
	    
	    if (size!=0){
	    	return true;
	    }
		return false;
		// end-user-code
	}
	
	public Part findPartByCode(int partCode) {
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select part from Part part where partCode=:insertedPartCode");
	    query.setParameter("insertedPartCode", partCode);
	    List <Part> results = query.getResultList();
	    int size = results.size();
	    
	    if (size!=0){
	    	return results.get(0);
	    }
		return null;
		// end-user-code
	}
	
	public Part findPartByCodeUpdate(int partCode) {
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("select part from Part part where partCode=:insertedPartCode");
	    query.setParameter("insertedPartCode", partCode);
	    List <Part> results = query.getResultList();
	    int size = results.size();
	    
	    if (size!=0){
	    	
	    	return results.get(0);
	    }
		return null;
		// end-user-code
	}
	
	public Boolean updatePartByCode(int partCode, String partName, String company, String manufacturyYear, double price ) {
		EntityManager em = JPAUtil.getCurrentEntityManager();
		Query query = em.createQuery("Update Part part SET partName=:insertedPartName, company=:insertedCompany, manufacturyYear=:insertedYear, price=:insertedPrice  where partCode=:insertedPartCode");
	    query.setParameter("insertedPartCode", partCode);
	    query.setParameter("insertedCompany", company);
	    query.setParameter("insertedYear", manufacturyYear);
	    query.setParameter("insertedPrice", price);
	    query.setParameter("insertedPartName", partName);
	    int results = query.executeUpdate();
	    
	    if (results!=0){
	    	
	    	return true;
	    }
		return false;
		// end-user-code
	}
	
	public Set<Part> findAllParts(Deptype depCode) {
		// begin-user-code
		// TODO Auto-generated method stub
	    // EntityManager em = JPAUtil.getCurrentEntityManager();
			//EntityManager em = DBUtilities.getEntityManager();
			//Query query = em.createQuery("select part from Part part where depCode=:insertedDepCode");
		    //query.setParameter("insertedDepCode", depCode.toString());
		   // List <Part> results = query.getResultList();
		   /// em.close();
			//Set <Part> allParts = query.getResultList();
		   // return allParts;
			// end-user-code
		
		CriteriaBuilder builder = JPAUtil.getEntityManagerFactory().getCriteriaBuilder();
	     CriteriaQuery<Part> criteria = builder.createQuery( Part.class);
	     Root<Part> part = criteria.from( Part.class );
	     criteria.select(part);
	     List<Part> allparts = JPAUtil.getCurrentEntityManager().createQuery(criteria).getResultList();
	     Set<Part> parts = new HashSet(allparts);
	     return parts;
	     // end-user-code
		}
	
	public Set<Part> findAllPartsCode(Deptype depCode) {
		// begin-user-code
		// TODO Auto-generated method stub
	    EntityManager em = JPAUtil.getCurrentEntityManager();
		//EntityManager em = DBUtilities.getEntityManager();
		Query query = em.createQuery("select part from Part part where depCode=:insertedDepCode");
		query.setParameter("insertedDepCode", depCode.toString());
		List<Part> results = query.getResultList();
		Set <Part> allParts =  new HashSet(results);
		
		    return allParts;
			// end-user-code
	     // end-user-code
		}

	/** 
	 * <!-- begin-UML-doc -->
	 * <!-- end-UML-doc -->
	 * @param part
	 * @generated "UML to Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
	 */
	public void save(Part part) {
		// begin-user-code
		// TODO Auto-generated method stub
		super.save(part);
		// end-user-code
	}
	
	public Set<Part> findAllPartsOfTheCompany() {
		// begin-user-code
		// TODO Auto-generated method stub
	     CriteriaBuilder builder = JPAUtil.getEntityManagerFactory().getCriteriaBuilder();
	     CriteriaQuery<Part> criteria = builder.createQuery( Part.class );
	     Root<Part> part = criteria.from( Part.class );
	     criteria.select(part);
	     List<Part> allparts = JPAUtil.getCurrentEntityManager().createQuery(criteria).getResultList();
	     Set<Part> parts = new HashSet(allparts);
	     return parts;
}
}